//
//  SearchSelectTableview.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/4/29.
//  Copyright © 2016年 game. All rights reserved.
//

#import "SearchSelectTableview.h"
#import "SelectionCell.h"
#import "SpaceModel.h"
#import "SeleCtContrl.h"
#import "LoginController.h"
#import "OtherInformationCtrl.h"
#import "MJRefresh.h"
@interface SearchSelectTableview ()<UITableViewDelegate,UITableViewDataSource,SelectionCellDeleget>
@property (nonatomic,strong) SpaceModel *model;
@property (nonatomic,strong) NSMutableArray *dataAry;

@property (nonatomic, strong) NSCache * cellCache;
@end

@implementation SearchSelectTableview

-(NSCache *)cellCache{
    if (!_cellCache) {
        _cellCache = [NSCache new];
    }
    return _cellCache;
}


-(NSMutableArray *)dataAry{

    if (!_dataAry) {
        _dataAry = [NSMutableArray array];
    }
    return _dataAry;
}

-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{

    if (self = [super initWithFrame:frame style:style]) {
        
        self.dataSource = self;
        self.delegate = self;
        self.separatorStyle = 0;
        [self registerNib:[UINib nibWithNibName:@"SelectionCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        self.showsVerticalScrollIndicator = NO;
        
        self.footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(LoadMorelData)];
        self.footer.automaticallyHidden = YES;
        
        __weak typeof(self) weakSelf = self;
        self.TapBlok = ^{
            
            [weakSelf setSearchString:weakSelf.searchString];
        };

        
    }
    return self;
}

-(void)setSearchString:(NSString *)searchString{
    
    _searchString = searchString;
    [self.dataAry removeAllObjects];
    [self GetFormHashAndpage:1];
    
}

-(void)GetFormHashAndpage:(NSInteger )page{

    [[Manager sharedManager]RequestWhithURL:GetFormhash AndParameter:nil AndCompletionType:^(id responseObject, DIEError *error) {
        if ([responseObject[@"status"] isEqualToString:@"SUCCESS"]) {
            
            [self SearchWhithFormhash:responseObject[@"data"][@"formhash"] AndString:_searchString andPage:page];
            
        }
    }];


}


-(void)SearchWhithFormhash:(NSString *)formhash AndString:(NSString *)string andPage:(NSInteger )page{
    
    
    NSDictionary *dic = @{
                          @"srchtxt":string,
                          @"formhash":formhash,
                          @"page":[NSString stringWithFormat:@"%ld",(long)page],

                          };
    
    [[Manager sharedManager]ResponseWhithURL:selectSearch AndParameter:dic AndCompletionType:^(id responseObject, DIEError *error) {
        
        if (!error) {
            if ([responseObject[@"status"] isEqualToString:@"SUCCESS"]) {
                
                self.model = [[SpaceModel alloc]initWithDictionary:responseObject[@"data"] error:nil];
                
                [self.dataAry addObjectsFromArray:self.model.thread_list];
                
                
                [self.footer endRefreshing];
                self.isShowImage = YES;
                [self reloadData];

                
               
            }else{
                [self.footer endRefreshing];
                self.isShowImage = YES;
                [self reloadData];
            }
        }else{
            
            [self.footer endRefreshing];
            self.isShowFile = YES;
            [self reloadData];
        }

            }];
    
}

#pragma mark---上拉加载
-(void)LoadMorelData{
    
     [self.cellCache removeAllObjects];
    SpacePagerModel *pageModel  = self.model.pager;
    if ((pageModel.cur_page.integerValue+1)==pageModel.total_page.integerValue) {
        [self.footer noticeNoMoreData];
    }else
        [self GetFormHashAndpage:pageModel.cur_page.integerValue+1];
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.dataAry.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.size.height ;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
     NSString *key = [NSString stringWithFormat:@"%ld-%ld",(long)indexPath.section, (long)indexPath.row];
    SelectionCell *cell = [self.cellCache objectForKey:key];
    if (!cell) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        cell.deleget = self;
        [self.cellCache setObject:cell forKey:key];
    }
   
    
    if (self.dataAry.count!=0) {
         cell.model = self.dataAry[indexPath.row];
    }
    return cell;
}
#pragma mark--SelectionCell中的代理方法
-(void)CommentSelect:(NSString *)tid Row:(NSInteger)row{
    NSString *url = [NSString stringWithFormat:MainSelet,tid];
    
    SeleCtContrl *str = [SeleCtContrl new];
    str.tid = tid;
    str.url = url;
    str.isSlide = YES;
    str.rowOfCell = row;
    [self.fatherCtrl.navigationController pushViewController:str animated:YES];
}

-(void)UserSpaceClick:(NSString *)uid{
    OtherInformationCtrl *space = [OtherInformationCtrl new];
    space.uid = uid;
    [self.fatherCtrl.navigationController pushViewController:space animated:YES];
}

-(void)PramisNoLogin{
    LoginController *login = [LoginController new];
    [self.fatherCtrl.navigationController pushViewController:login animated:YES];
}

#pragma mark--tableview点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    SelectionCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *url = [NSString stringWithFormat:MainSelet,cell.tid];
    
    SeleCtContrl *str = [SeleCtContrl new];
    str.tid = cell.tid;
    str.url = url;
    str.isSlide = NO;
    str.rowOfCell = indexPath.row;
    
    [self.fatherCtrl.navigationController pushViewController:str animated:YES];
}

-(void)dealloc{

    [self.cellCache removeAllObjects];
}
@end
